<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';

import { message, TreeSelect } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { postAdminMenus, putAdminMenus } from '#/api/admin/menus';

const menus = ref();
const [Form, formApi] = useVbenForm({
  // 所有表单项共用，可单独在表单内覆盖
  commonConfig: {
    labelClass: 'w-2/6',
  },
  handleSubmit: onSubmit,
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'TreeSelect',
      fieldName: 'parent_id',
      label: '上级菜单',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'name',
      label: '菜单名称',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'app',
      label: '应用',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'controller',
      label: '控制器',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'action',
      label: '方法',
      rules: 'required',
    },
    {
      defaultValue: '',
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'param',
      label: '参数',
    },
    {
      component: 'InputIcon',
      fieldName: 'icon',
      label: '图标',
    },
    {
      component: 'Switch',
      componentProps: {
        placeholder: '请输入',
        checkedValue: 1,
        unCheckedValue: 0,
        checkedChildren: '显示',
        unCheckedChildren: '隐藏',
      },
      fieldName: 'status',
      label: '状态',
      rules: 'required',
    },
    {
      component: 'Select',
      defaultValue: '',
      componentProps: {
        class: 'w-full',
        placeholder: '请选择',
        options: [
          {
            value: 1,
            label: '有界面可访问菜单',
          },
          {
            value: 2,
            label: '无界面可访问菜单',
          },
          {
            value: 0,
            label: '只作为菜单',
          },
        ],
      },
      fieldName: 'type',
      label: '类型',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'list_order',
      label: '排序',
    },
    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'remark',
      label: '备注',
    },
  ],
  showDefaultActions: false,
  wrapperClass: 'grid-cols-1 md:grid-cols-2',
});
const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  onCancel() {
    formModalApi.close();
  },
  onConfirm: async () => {
    await formApi.validateAndSubmitForm();
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: any = formModalApi.getData();
      if (values.form) {
        formApi.setValues(values.form);
      }
      menus.value = values.menus;
    }
  },
});

async function onSubmit(values: any) {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
    formModalApi.close();
  }
  if (values.icon) {
    values.icon = values.icon.replace(`fa:`, '');
  }
  if (values.id) {
    const { msg } = await putAdminMenus(values.id, values);
    showMsg(msg);
  } else {
    const { msg } = await postAdminMenus(values);
    showMsg(msg);
  }
}
</script>
<template>
  <Modal class="w-3/5">
    <Form>
      <template #parent_id="slotProps">
        <TreeSelect
          v-bind="slotProps"
          :field-names="{
            value: 'id',
            key: 'id',
            label: 'name',
            children: 'children',
          }"
          :tree-data="menus"
          allow-clear
          placeholder="选择导航"
          show-search
          style="width: 100%"
          tree-node-filter-prop="label"
        />
      </template>
    </Form>
  </Modal>
</template>

<style lang="scss" scoped></style>
